package com.royal.lr.interview;

import java.util.Stack;

public class StackScene {
    /**
     * 单链表，求倒数第K个数
     * @param node
     * @param k
     * @return
     */
    public static int getSingleListLastKNum(MyNode node,int k){
        Stack<MyNode> stacks=new Stack<>();
        while(node!=null){
            stacks.push(node);
            node=node.next;
        }
        MyNode target=null;
        int size=stacks.size();
        while(stacks.size()>0){
            target=stacks.pop();
            int subSize=stacks.size();
            if(size-subSize==k){
                break;
            }
        }
        return target.value;
    }


    /**
     *单链表，判断其是否为回文数
     * @param node
     * @return
     */
    public static boolean isPalindromeNum(MyNode node){
        boolean flag=true;
        MyNode originalNode=node;
        Stack<MyNode> stacks=new Stack<>();
        while(node!=null){
            stacks.push(node);
            node=node.next;
        }
        while(stacks.size()>0){
            Integer value=stacks.pop().value;
            Integer oriVal=originalNode.value;
            if(value!=oriVal){
                flag=false;
                break;
            }
            originalNode=originalNode.next;
        }
        return flag;
    }
}
